﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRAdminUI.EmpModule
{
    public partial class AddEmployeePersonalData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetNewEmployeeCode();
                LoadDesignation();
                LoadProject();
                Loadcatagory();
                // LoadStatus();
                //btnNext.Visible = false;

            }


        }

        private void GetNewEmployeeCode()
        {
            Code code = new Code();
            CodeManager manager = new CodeManager();
            code = manager.GenerateNewCode();
            employeeCode.InnerText = code.CodeName;
        }

        private void LoadDesignation()
        {
            List<Designation> designations = new List<Designation>();
            DesignationManager manager = new DesignationManager();
            designations = manager.GetAllDesignations();
            ddlEmpdesignation.DataTextField = "Title";
            ddlEmpdesignation.DataValueField = "Id";
            ddlEmpdesignation.DataSource = designations;
            ddlEmpdesignation.DataBind();
            ddlEmpdesignation.Items.Insert(0, "---Choose Any---");
        }

        private void LoadProject()
        {
            List<Project> projects = new List<Project>();
            ProjectManager manager = new ProjectManager();
            projects = manager.GetAllProjects();
            ddlEmpProject.DataTextField = "ProjectName";
            ddlEmpProject.DataValueField = "Id";
            ddlEmpProject.DataSource = projects;
            ddlEmpProject.DataBind();
            ddlEmpProject.Items.Insert(0, "---Choose Any---");

        }

        private void Loadcatagory()
        {
            List<Catagory> catagories = new List<Catagory>();
            CatagoryManager manager = new CatagoryManager();
            catagories = manager.GetAllCatagories();
            ddempCatagory.DataTextField = "EmpCatagory";
            ddempCatagory.DataValueField = "Id";
            ddempCatagory.DataSource = catagories;
            ddempCatagory.DataBind();
            ddempCatagory.Items.Insert(0, "---Choose Any---");

        }

        protected void SaveEmployee(Object o, EventArgs e)
        {
            try
            {
                Employee employee = new Employee();
                EmployeeManager manager = new EmployeeManager();

                Code code = new Code();
                Designation designation = new Designation();
                Project project = new Project();
                Catagory catagory = new Catagory();

                employee.Name = txtName.Value;
                employee.FathersName = txtFname.Value;
                employee.MothersName = txtMname.Value;
                employee.Sex = ddlsex.Value;
                employee.Blood = ddbloodgroup.Value;
                employee.DOB = Convert.ToDateTime(txtDob.Value);
                employee.Email = txtEmail.Value;
                employee.Nationality = txtnationality.Value;
                employee.NID = txtNationalID.Value;
                designation.Id = Convert.ToInt32(ddlEmpdesignation.Value);
                employee.Designation = designation;
                employee.Skill = txtSkill.Value;
                employee.JoiningDate = Convert.ToDateTime(txtJoiningDate.Value);
                if (txtContatFinishDate.Value != "")
                {
                    employee.ContractFinishDate = Convert.ToDateTime(txtContatFinishDate.Value);

                }
                else
                {
                    employee.ContractFinishDate = Convert.ToDateTime("01-01-1800");
                }

                project.Id = Convert.ToInt32(ddlEmpProject.Value);
                employee.Project = project;

                catagory.Id = Convert.ToInt32(ddempCatagory.Value);
                employee.EmpCatagoryId = catagory;
                employee.Status = ddStatus.Value;
                employee.Education = txtEducation.Value;
                code.CodeName = employeeCode.InnerText;
                employee.EmpCode = code;
                string resumeName = Path.GetFileName(uploadresume.PostedFile.FileName);
                uploadresume.PostedFile.SaveAs(Server.MapPath("~/Resume/" + code.CodeName + "_" + resumeName));
                employee.Resume = code.CodeName + "_" + resumeName;
                if (manager.SaveEmployee(employee))
                {
                    ClearInput();
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = "Employee Saved Successfully !";
                    Session["empCode"] = employeeCode.InnerText;
                    btnNext.Visible = true;
                    //  GetNewEmployeeCode();

                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = "Employee Not Saved !";
                }

            }
            catch (Exception ex)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = ex.Message;
            }
        }

        protected void ClearInput()
        {
            txtName.Value = "";
            txtContatFinishDate.Value = "";
            txtDob.Value = "";
            txtEducation.Value = "";
            txtEmail.Value = "";
            txtFname.Value = "";
            txtMname.Value = "";
            txtNationalID.Value = "";
            txtSkill.Value = "";
            txtnationality.Value = "";
            ddStatus.Value = "";
            ddbloodgroup.Value = "";
            ddempCatagory.Value = "";
            ddlEmpProject.Value = "";
            ddlsex.Value = "";
            txtJoiningDate.Value = "";



        }
    }
}